/**
 * VUEshop
 * ============================================================================
 * * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.vueshop.com.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: soubao-java 2020-07-22 $
 */<template>
  <div>
    <div
      id="explanation"
      class="explanation"
      style="color: rgb(85, 85, 85); background-color: rgb(243, 244, 244); width: 98%; height: 100%;margin-bottom:15px"
    >
      <div class="bckopa-tips">
        <div class="title">
          <img src="@/static/images/handd.png" alt />
          <h4 title="提示相关设置操作时应注意的要点">操作提示</h4>
        </div>
        <ul>
          <li>配置微信公众号, token、Appid、AppSecret要与微信公众开放平台信息一致</li>
        </ul>
      </div>
      <span title="收起提示" id="explanationZoom" style="display: block;"></span>
    </div>
    <el-form ref="form" :rules="rules" :model="form" label-width="200px">
      <el-form-item label="URL（服务器地址）">
        <el-tag v-if="shopInfo.host">{{shopInfo.host}}/mall/wechat</el-tag>
        <p v-else class="my-notic">配置<router-link :to="{name:'shop_info'}">接口域名</router-link>后显示</p>
      </el-form-item>
      <el-form-item label="Token" prop="mutual_token">
        <el-input v-model="form.mutual_token" size="mini"></el-input>
        <p class="notic">1. Token相当于腾讯微信公众平台服务器与你服务器交互的密钥</p>
        <p class="notic">2. 在正式部署的时候，请务必设置得较为复杂，否则可能被黑客利用，伪装你的服务器向你的用户发送消息</p>
      </el-form-item>
      <el-form-item label="公众号名称" prop="wxname">
        <el-input v-model="form.wxname" size="mini"></el-input>
      </el-form-item>
      <el-form-item label="公众号原始ID" prop="wxid">
        <el-input v-model="form.wxid" size="mini"></el-input>
      </el-form-item>
      <el-form-item label="微信号" prop="weixin">
        <el-input v-model="form.weixin" size="mini"></el-input>
      </el-form-item>
      <el-form-item label="头像" prop="headerpic">
        <el-upload
          class="avatar-uploader"
          :action="apiHead + '/mall/upload?type=weixin/head_pic'"
          :show-file-list="false"
          :on-success="handleHeaderPic"
          :before-upload="beforeAvatarUpload"
        >
          <el-image
            v-if="form.headerpic"
            :src="form.headerpic"
            style="width:90px; height:90px; display:block;"
          ></el-image>
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="AppID" prop="appid">
        <el-input v-model="form.appid" size="mini"></el-input>
      </el-form-item>
      <el-form-item label="AppSecret" prop="appsecret">
        <el-input v-model="form.appsecret" size="mini"></el-input>
      </el-form-item>
      <el-form-item label="二维码" prop="qr">
        <el-upload
          class="avatar-uploader"
          :action="apiHead + '/mall/upload?type=weixin/qrcode'"
          :show-file-list="false"
          :on-success="handleQR"
          :before-upload="beforeAvatarUpload"
        >
          <el-image v-if="form.qr" :src="form.qr" style="width:90px; height:90px; display:block;"></el-image>
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
        <p class="notic">1. 此处上传公众号二维码,建议上传90*高90像素GIF\JPG\PNG格式图片</p>
        <p class="notic">2. 此处二维码将会在PC首页侧边栏（右下角）展示</p>
      </el-form-item>
      <el-form-item label="微信号类型" prop="type">
        <el-select v-model="form.type" size="mini">
          <el-option
            v-for="item in types"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="微信接入状态" prop="wait_access">
        <el-radio-group v-model="form.wait_access">
          <el-radio :label="0">等待接入</el-radio>
          <el-radio :label="1">已接入</el-radio>
        </el-radio-group>
        <p class="err-msg">
          重要：任何时候都要勾选"已接入"，否则微信功能无法正常使用。 除非，在接入微信（开发 ->基本配置 -> 修改配置）提交url和token
          提示'url超时'或'token'错误时，此处临时勾选为"等待接入"，提交成功后改为"已接入"
        </p>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit('form')" :loading="isLoading">确认提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
	import { getConfigs } from "@/utils/api";
export default {
  data() {
    return {
		shopInfo: {},
      form: {
        type: 1,
        wait_access: 1,
        uid: 1,
        headerpic: "",
        token: "",
        share_dated: "",
        authorizer_expires: "",
        web_expires: 0
      },
      types: [
        {
          label: "订阅号",
          value: 1
        },
        {
          label: "认证订阅号",
          value: 2
        },
        {
          label: "服务号",
          value: 3
        },
        {
          label: "认证服务号",
          value: 4
        }
      ],
      rules: {
        host: [{ required: true, message: "请输入网站域名", trigger: "blur" }],
        mutual_token: [
          { required: true, message: "请输入Token", trigger: "blur" }
        ],
        wxname: [
          { required: true, message: "请输入公众号名称", trigger: "blur" }
        ],
        wxid: [
          { required: true, message: "请输入公众号原始ID", trigger: "blur" }
        ],
        weixin: [{ required: true, message: "请输入微信号", trigger: "blur" }],
        appid: [{ required: true, message: "请输入AppID", trigger: "blur" }],
        appsecret: [
          { required: true, message: "请输入AppSecret", trigger: "blur" }
        ]
      },
      isLoading: false
    };
  },
  created() {
    this.getConfigs();
	this.getShopConfigs()
  },
  methods: {
	getShopConfigs() {
	  var that = this;
	  getConfigs({ inc_type: "shop_info" }).then(function(res) {
	    that.shopInfo = res.shop_info;
	  });
	},
    getConfigs() {
      var that = this;
      this.request.get("/mall/wx_user").then(function(res) {
        if (res) {
          that.form = res;
        }
      });
    },
    onSubmit(formName) {
      let check = true;
      this.$refs[formName].validate(valid => {
        if (!valid) {
          check = false;
          return false;
        }
      });
      if (!check) {
        this.$message.error("参数错误");
        return;
      }
      var that = this;
      this.isLoading = true;
      if (this.form.id) {
        //更新
        this.request.put("/mall/wx_user", that.form).then(function(res) {
          if (res.status == 1) {
            that.$message.success("更新成功");
            that.getConfigs();
          } else {
            that.$message.error("更新失败,失败原因:" + res.msg);
          }
        });
      } else {
        //添加
        this.request.post("/mall/wx_user", that.form).then(function(res) {
          if (res.status == 1) {
            that.$message.success("保存成功");
            that.getConfigs();
          } else {
            that.$message.error("保存失败,失败原因:" + res.msg);
          }
        });
      }
      that.isLoading = false;
    },
    handleHeaderPic(res) {
      if (res.status < 1) {
        this.$message.error(res.msg);
        return;
      }
      this.form.headerpic = res.result;
      this.$forceUpdate();
    },
    handleQR(res) {
      if (res.status < 1) {
        this.$message.error(res.msg);
        return;
      }
      this.form.qr = res.result;
      this.$forceUpdate();
    },
    beforeAvatarUpload(file) {
      const isJPG =
        file.type === "image/gif" ||
        file.type === "image/jpg" ||
        file.type === "image/jpeg" ||
        file.type === "image/png";
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isJPG) {
        this.$message.error("上传图片只能是 JPG、JPEG、PNG、GIF 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    }
  }
};
</script>
<style scoped>
.avatar-uploader-icon {
  line-height: 100px;
}
>>> .el-input__inner {
  width: 300px;
}
/* 提示文字样式 */
.my-notic {
  color: #aaa;
  font-size: 10px;
}
.my-notic:hover {
  color: #148eff;
}
</style>
